﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;
namespace DAO
{
    /// <summary>
    /// Summary description for Theme
    /// </summary>
    public class Theme
    {
        DBConnection DBConnection;
        public Theme()
        {
            DBConnection = new DBConnection();
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Get all DTO.Theme instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.Theme instances.</returns>
        public List<DTO.Theme> GetAllThemes()
        {
            Log.LogInfo("Begin GetAllThemes()");
            String query = "SELECT * FROM [Theme];";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            List<DTO.Theme> themeDTOs = new List<DTO.Theme>();
            try
            {
                while (reader.Read())
                {
                    themeDTOs.Add(new DTO.Theme(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetAllThemes(): " + ex.StackTrace);
            }
            DBConnection.Close();
            Log.LogInfo("End GetAllThemes()");
            return themeDTOs;
        }
        /// <summary>
        /// Get DTO.Theme instance from DB.
        /// </summary>
        /// <param name="ID">Theme ID</param>
        /// <returns>DTO.Theme instance which has the specific ID.</returns>
        public DTO.Theme GetTheme(String ID)
        {
            Log.LogInfo("Begin GetTheme(String ID)");
            String query = "SELECT * FROM [Theme] WHERE [Theme].[ID]='" + ID + "';";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            DTO.Theme themeDTO = null;
            try
            {
                if (reader.Read())
                    themeDTO = new DTO.Theme(reader);
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetTheme(String ID): " + ex.Message);
            }
            DBConnection.Close();
            Log.LogInfo("End GetTheme(String ID)");
            return themeDTO;
        }

        /// <summary>
        /// Insert Theme into database.
        /// </summary>
        /// <param name="Theme">DTO.Theme instance to insert into database.</param>
        public int InsertTheme(DTO.Theme Theme)
        {
            Log.LogInfo("Begin InsertTheme()");
            String query = "";
            query += "INSERT INTO THEME (Name, FilePath, Image) VALUES ('" + Theme.Name + "','" + Theme.FilePath + "','" + Theme.Image + "')";
            query += "";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertTheme()");
            return result;
        }

        /// <summary>
        /// Update Theme info into database.
        /// </summary>
        /// <param name="Theme">Theme instance to be updated.</param>
        public int UpdateTheme(DTO.Theme Theme)
        {
            Log.LogInfo("Begin UpdateTheme()");
            String query = "UPDATE MESSAGE SET Name = '" + Theme.Name + "'," + " FilePath = '" + Theme.FilePath + "'Image ='" + Theme.Image + "' WHERE ID = '" + Theme.ID + "'";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertTheme()");
            return result;
        }
        /// <summary>
        /// Delete a specific Theme from DB.
        /// </summary>
        /// <param name="Theme">The Theme to be deleted.</param>
        public int DeleteTheme(DTO.Theme Theme)
        {
            Log.LogInfo("Begin DeleteTheme()");
            String query = "DELETE FROM [Theme] WHERE [Theme].[ID]='" + Theme.ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteTheme()");
            return result;
        }

        /// <summary>
        /// Delete a Theme in database using ID.
        /// </summary>
        /// <param name="ID">Theme ID.</param>
        public int DeleteTheme(String ID)
        {
            Log.LogInfo("Begin DeleteTheme()");
            String query = "DELETE FROM [Theme] WHERE [Theme].[ID]='" + ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteTheme()");
            return result;
        }
    }
}